package org.bigdata.framework.core.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import net.sf.json.JSONObject;

import javax.websocket.*;

//在相对路径中发布端点websocket
//@ServerEndpoint("/ws/{onlineId}")
public class WebSocketServlet {
	// 用来存放每个客户端对应的MyWebSocket对象。
	private static Map<String,CopyOnWriteArraySet<WebSocketServlet>>webSocketMap=new HashMap<String,CopyOnWriteArraySet<WebSocketServlet>>();
	private javax.websocket.Session session = null;
	private static Map<String,Integer>onlineNumber=new HashMap<String, Integer>();
	/**
	 * @ClassName: onOpen
	 * @Description: 开启连接的操作
	 */
	@OnOpen
	public void onOpen(Session session,@PathParam(value = "onlineId") String onlineId) throws IOException {
		this.session = session;
		if(webSocketMap.get(onlineId)==null){
			CopyOnWriteArraySet<WebSocketServlet> webSocketSet = new CopyOnWriteArraySet<WebSocketServlet>();
			webSocketSet.add(this);
			webSocketMap.put(onlineId, webSocketSet);
			webSocketSet=null;
		}else{
			CopyOnWriteArraySet<WebSocketServlet> webSocketSet = webSocketMap.get(onlineId);
			if(webSocketSet!=null){
				webSocketSet.add(this);
				webSocketMap.put(onlineId, webSocketSet);
			}
			webSocketSet=null;
		}
		addOnlineCount(onlineId);
	}

	/**
	 * @ClassName: onClose
	 * @Description: 连接关闭的操作
	 */
	@OnClose
	public void onClose(@PathParam(value = "onlineId") String onlineId) {
		if(webSocketMap.get(onlineId)!=null){
			CopyOnWriteArraySet<WebSocketServlet> webSocketSet = webSocketMap.get(onlineId);
			if(webSocketSet!=null){
				webSocketSet.remove(this);
				webSocketMap.put(onlineId, webSocketSet);
				subOnlineCount(onlineId);
			}
			webSocketSet=null;
		}
	}

	/**
	 * @ClassName: onMessage
	 * @Description: 给服务器发送消息告知数据库发生变化
	 */
	@OnMessage
	public void onMessage(String content,@PathParam(value = "onlineId") String onlineId) {
		try {
			sendMessage(content,onlineId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @ClassName: OnError
	 * @Description: 出错的操作
	 */
	@OnError
	public void onError(Throwable error) {
		System.out.println(error);
		error.printStackTrace();
	}

	/**
	 * 这个方法与上面几个方法不一样。没有用注解，是根据自己需要添加的方法。
	 * 
	 * @throws IOException
	 *             发送自定义信号，“1”表示告诉前台，数据库发生改变了，需要刷新
	 */
	public void sendMessage(String content,String onlineId) throws IOException {
		if(webSocketMap.get(onlineId)!=null){
			CopyOnWriteArraySet<WebSocketServlet> webSocketSet = webSocketMap.get(onlineId);
			if(webSocketSet!=null){
				for (WebSocketServlet item : webSocketSet) {
					JSONObject o = JSONObject.fromObject(content);
					o.put("onlineNumber", getOnlineCount(onlineId));
					item.session.getBasicRemote().sendText(o.toString());
					o=null;
				}
			}
			webSocketSet=null;
		}
	}
	
	public static synchronized int getOnlineCount(String id) {
		return onlineNumber.get(id);
	}
	
	public static synchronized  Map<String,Integer> getAllOnlineCount(String id) {
		return onlineNumber;
	}

	public static synchronized void addOnlineCount(String id) {
		if(onlineNumber.get(id)==null){
			onlineNumber.put(id, 1);
		}else{
			onlineNumber.put(id, onlineNumber.get(id)+1);
		}
	}

	public static synchronized void subOnlineCount(String id) {
		if(onlineNumber.get(id)==null||onlineNumber.get(id)<1){
			onlineNumber.put(id, 0);
		}else{
			onlineNumber.put(id, onlineNumber.get(id)-1);
		}
	}
}
